package org.example.server.tcp;

/**
 * @Title: TcpServerHandler
 * @Author: kunliangzhao
 * @Package org.example.server.tcp
 * @Date: 2025/6/20 17:32
 * @Description: 请求处理器
 */

import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetSocket;
import org.example.model.RpcRequest;
import org.example.model.RpcResponse;
import org.example.protocol.ProtocolMessage;
import org.example.protocol.ProtocolMessageDecoder;
import org.example.protocol.ProtocolMessageEncoder;
import org.example.protocol.ProtocolMessageTypeEnum;
import org.example.registry.LocalRegistry;

import java.io.IOException;
import java.lang.reflect.Method;

public class TcpServerHandler implements Handler<NetSocket> {

//    @Override
//    public void handle(NetSocket netSocket) {
//        // 处理连接
//        netSocket.handler(buffer -> {
//            // 接受请求，解码
//            ProtocolMessage<RpcRequest> protocolMessage;
//            try {
//                protocolMessage = (ProtocolMessage<RpcRequest>) ProtocolMessageDecoder.decode(buffer);
//            } catch (IOException e) {
//                throw new RuntimeException("协议消息解码错误");
//            }
//            RpcRequest rpcRequest = protocolMessage.getBody();
//
//            // 处理请求
//            // 构造响应结果对象
//            RpcResponse rpcResponse = new RpcResponse();
//            try {
//                // 获取要调用的服务实现类，通过反射调用
//                Class<?> implClass = LocalRegistry.get(rpcRequest.getServiceName());
//                Method method = implClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
//                Object result = method.invoke(implClass.newInstance(), rpcRequest.getArgs());
//                // 封装返回结果
//                rpcResponse.setData(result);
//                rpcResponse.setDataType(method.getReturnType());
//                rpcResponse.setMessage("ok");
//            } catch (Exception e) {
//                e.printStackTrace();
//                rpcResponse.setMessage(e.getMessage());
//                rpcResponse.setException(e);
//            }
//
//            // 发送响应，编码
//            ProtocolMessage.Header header = protocolMessage.getHeader();
//            header.setType((byte) ProtocolMessageTypeEnum.RESPONSE.getKey());
//            ProtocolMessage<RpcResponse> responseProtocolMessage = new ProtocolMessage<>(header, rpcResponse);
//            try {
//                Buffer encode = ProtocolMessageEncoder.encode(responseProtocolMessage);
//                netSocket.write(encode);
//            } catch (IOException e) {
//                throw new RuntimeException("协议消息编码错误");
//            }
//        });
//    }

    @Override
    public void handle(NetSocket netSocket) {
        TcpBufferHandlerWrapper bufferHandlerWrapper = new TcpBufferHandlerWrapper(
                buffer -> {
                    // 处理请求
                    // 接受请求，解码
                    ProtocolMessage<RpcRequest> protocolMessage;
                    try {
                        protocolMessage = (ProtocolMessage<RpcRequest>) ProtocolMessageDecoder.decode(buffer);
                    } catch (IOException e) {
                        throw new RuntimeException("协议消息解码错误");
                    }
                    RpcRequest rpcRequest = protocolMessage.getBody();

                    // 处理请求
                    // 构造响应结果对象
                    RpcResponse rpcResponse = new RpcResponse();
                    try {
                        // 获取要调用的服务实现类，通过反射调用
                        Class<?> implClass = LocalRegistry.get(rpcRequest.getServiceName());
                        Method method = implClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
                        Object result = method.invoke(implClass.newInstance(), rpcRequest.getArgs());
                        // 封装返回结果
                        rpcResponse.setData(result);
                        rpcResponse.setDataType(method.getReturnType());
                        rpcResponse.setMessage("ok");
                    } catch (Exception e) {
                        e.printStackTrace();
                        rpcResponse.setMessage(e.getMessage());
                        rpcResponse.setException(e);
                    }

                    // 发送响应，编码
                    ProtocolMessage.Header header = protocolMessage.getHeader();
                    header.setType((byte) ProtocolMessageTypeEnum.RESPONSE.getKey());
                    ProtocolMessage<RpcResponse> responseProtocolMessage = new ProtocolMessage<>(header, rpcResponse);
                    try {
                        Buffer encode = ProtocolMessageEncoder.encode(responseProtocolMessage);
                        netSocket.write(encode);
                    } catch (IOException e) {
                        throw new RuntimeException("协议消息编码错误");
                    }
                }
        );
        netSocket.handler(bufferHandlerWrapper);
    }
}

